package com.woniuxy.rbac.web.api;

import com.woniuxy.rbac.entity.Perm;
import com.woniuxy.rbac.entity.ResponseResult;
import com.woniuxy.rbac.service.PermService;
import com.woniuxy.rbac.utils.Audience;
import com.woniuxy.rbac.utils.JwtUtil;
import com.woniuxy.rbac.vo.PermVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 老谭（<a href="http://www.woniuxy.com">蜗牛学苑</a>）
 */
@RestController
@RequestMapping("/perm")
public class PermController {
    @Autowired
    private PermService permService;
    @Autowired
    private Audience audience;

    @GetMapping("getforuser")
    public ResponseResult<List<Perm>> getPermsById(@RequestHeader("token") String jwt) {
        int userId = JwtUtil.parseJwtForId(jwt, audience);
        return new ResponseResult<>(permService.getPermsByUserId(userId));
    }

    @GetMapping("getmenusforuser")
    public ResponseResult<List<Perm>> getMenussById(@RequestHeader("token") String jwt) {
        int userId = JwtUtil.parseJwtForId(jwt, audience);
        return new ResponseResult<>(permService.getMenusByUserId(userId));
    }

    @GetMapping("getbyroleid")
    public ResponseResult<List<Perm>> getByRoleId(int roleId) {
        return new ResponseResult<>(permService.getPermsByROleId(roleId));
    }

    //    @GetMapping("getallperms")
//    public ResponseResult<List<PermVO>> getAll() {
//        List<Perm> perms = permService.getPerms();
//        List<PermVO> permVOs = new ArrayList<>();
//        //转换
//        PermVO permVO1 = new PermVO();
//        permVO1.setId(1);
//        permVO1.setLabel("权限1");
//        PermVO permVO2 = new PermVO();
//        permVO2.setId(2);
//        permVO2.setLabel("权限2");
//
//        PermVO permVO3 = new PermVO();
//        permVO3.setId(3);
//        permVO3.setLabel("权限3");
//
//        permVO1.setChildren(Arrays.asList(permVO2, permVO3));
//        permVOs.add(permVO1);
//        return new ResponseResult<>(permVOs);
//
//    }

    @GetMapping("getallperms")
    public ResponseResult<List<PermVO>> getAllMenus() {
        List<Perm> perms = permService.getPerms();
        List<PermVO> permVOs = new ArrayList<>();

        perms.forEach(p -> {
            if (p.getParentid() == 0) {   //顶级菜单/权限
                PermVO permVO = new PermVO();
                permVO.setId(p.getId());
                permVO.setLabel(p.getName());
                permVO.setChildren(new ArrayList<PermVO>());
                permVOs.add(permVO);
            }
        });

        permVOs.forEach(pvo -> {
            perms.forEach(p -> {
                if (p.getParentid() == pvo.getId()) {  //子菜单
                    PermVO permVO = new PermVO();
                    permVO.setId(p.getId());
                    permVO.setLabel(p.getName());
                    pvo.getChildren().add(permVO);
                }
            });
        });
        return new ResponseResult<>(permVOs);
    }

    @GetMapping("all")
    public ResponseResult<List<PermVO>> getAll(){
        List<Perm> perms = permService.getAll();
        List<PermVO> permVOs = new ArrayList<>();

        perms.forEach(p -> {
            if (p.getParentid() == 0) {   //顶级菜单/权限
                PermVO permVO = new PermVO();
                permVO.setId(p.getId());
                permVO.setLabel(p.getName());
                permVO.setType(p.getType());
                permVO.setChildren(new ArrayList<PermVO>());
                permVOs.add(permVO);
            }
        });

        permVOs.forEach(pvo -> {
            perms.forEach(p -> {
                if (p.getParentid() == pvo.getId()) {  //子菜单
                    PermVO permVO = new PermVO();
                    permVO.setId(p.getId());
                    permVO.setLabel(p.getName());
                    permVO.setType(p.getType());
                    pvo.getChildren().add(permVO);
                }
            });
        });
        return new ResponseResult<>(permVOs);
    }

}
